package com.soup.lab.basic.other;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SimpleTestCase {

    @Test
    public void test() {
        System.out.println(Integer.toBinaryString(-1));
        System.out.println(Integer.toBinaryString(-1 << (Integer.SIZE - 3)));
        System.out.println(Integer.toBinaryString(0 << (Integer.SIZE - 3)));
        System.out.println(Integer.toBinaryString(1 << (Integer.SIZE - 3)));
        System.out.println(Integer.toBinaryString(2 << (Integer.SIZE - 3)));
        System.out.println(Integer.toBinaryString(3 << (Integer.SIZE - 3)));
        System.out.println(Integer.toBinaryString((1 << (Integer.SIZE - 3)) - 1));
        System.out.println(Math.pow(2, 29) - 1);
    }


    @Test
    public void test2() {
        Pattern p = Pattern.compile("^(100(\\.0{1,2})?)|([1-9]?\\d(\\.\\d{1,2})?)$");
        System.out.println(p.matcher("100").matches());
        System.out.println(p.matcher("100.00").matches());
        System.out.println(p.matcher("33").matches());
        System.out.println(p.matcher("33.2").matches());
        System.out.println(p.matcher("33.22").matches());
        System.out.println(p.matcher("3").matches());
        System.out.println(p.matcher("0").matches());
        System.out.println(p.matcher("3.22").matches());
        System.out.println(p.matcher("400").matches());
    }

    @Test
    public void test3() {
        System.out.println(new BigDecimal("22.33").multiply(new BigDecimal(100)).intValue());
        System.out.println(new BigDecimal("22.3").multiply(new BigDecimal(100)).intValue());
        System.out.println(new BigDecimal("2.33").multiply(new BigDecimal(100)).intValue());
        System.out.println(new BigDecimal("1").multiply(new BigDecimal(100)).intValue());
        System.out.println(new BigDecimal("100").multiply(new BigDecimal(100)).intValue());
    }

    @Test
    public void test4() {
        System.out.println(new BigDecimal("23").divide(new BigDecimal("3"), MathContext.DECIMAL32));
    }

    @Test
    public void test5() {
        System.out.println(Math.round(15.5));
        System.out.println(Math.round(14.5));
        System.out.println(Math.round(13.4));
        System.out.println(Math.round(12.4));
    }

    static final String template = "update park_access_door_sync set name = '%s' where status = 0 and device_id = '%s';";

    @Test
    public void test6() throws IOException {
        List<String> strings = Files.readAllLines(Paths.get("D:\\soup\\lab\\basic-lab\\src\\main\\resources\\t.txt"), StandardCharsets.UTF_8);
        for (String string : strings) {
            String[] s = string.split("\\s+");
            System.out.println(String.format(template, s[0], s[1]));
        }
    }

    @Test
    public void test7() {
        System.out.println(System.currentTimeMillis()/1000);
    }

    @Test
    public void test8() {
        Pattern p = Pattern.compile("^$|^(100(\\.0{1,2})?)|([1-9]?\\d(\\.\\d{1,2})?)$");
        System.out.println(p.matcher("").matches());
        System.out.println(p.matcher("100.00").matches());
        System.out.println(p.matcher("100.00").matches());
    }

    @Test
    public void test9() {
        double a = 5.6d + 7.8d;
        double b = 86.6d;
        System.out.println(a + b);
    }

    @Test
    public void test10() {
        System.out.println("+86 1239847238".replaceAll("[^0-9A-Za-z]+", ""));
    }

    @Test
    public void test11() throws IOException {
        URL url = new URL("https://pacas-login.pingan.com.cn/cas/PA003/ICORE_PTS/login");
        URLConnection connection = url.openConnection();
        try(BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            br.lines().forEach(System.out::println);
        }
    }

    @Test
    public void test12() {
        System.out.println(String.format("东院%02d号楼%02d单元%02d房", 11, 11, 11));
        System.out.println(String.format("东院%d号楼%d单元%d房", 1, 1, 1));


    }

    @Test
    public void test13() {
        List<String> list = Arrays.asList("123", "12345", "123456");
        list.sort((a,b) -> b.length()-a.length());
        System.out.println(list);


    }

    @Test
    public void test14() {
        System.out.println(extractOrganCodePath("01003001003007007079007012007007007004"));
    }

    private static List<String /* organCode */> extractOrganCodePath(String organCode) {
        int organCodeLength = organCode.length();
        if(organCodeLength <= 8) {
            return Collections.singletonList(organCode);
        }

        List<String> codes = new ArrayList<>();
        int times = (organCodeLength - 8) / 6;
        codes.add(organCode);
        for (int i = 0; i < times; i++) {
            codes.add(organCode.substring(0, organCodeLength - (i + 1)*6));
        }
        return codes;
    }

    @Test
    public void test15() {
        desensitizedCustName("明");
        desensitizedCustName("小明");
        desensitizedCustName("王小明");
        desensitizedCustName("四格互联");
        desensitizedCustName("窗前明月光");
        desensitizedCustName("夜上海白玫瑰");
        desensitizedCustName("天天想上幼儿园");
        desensitizedCustName("深圳市四格互联科技有限公司");
        desensitizedCustName("深圳市四格互联科技有限公司武汉事业部");
    }

    public static String desensitizedCustName(String custName) {
        String before = custName;
        if(StringUtils.isBlank(custName)) {
            return "";
        }
        custName = custName.trim();
        int l = custName.length();
        int desensitizedLength = Math.min(l/2, 6);
        String after = custName.substring(0, l-desensitizedLength) + StringUtils.repeat('*', desensitizedLength);
        System.out.printf("脱敏- before: [%s], after: [%s]\n", before, after);
        return after;
    }

    @Test
    public void test16() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.SECOND, 0);
        c.add(Calendar.MINUTE, -5);
        System.out.println(c.getTime());
    }

    @Test
    public void test17() {
        A a = new A("a", 10);
        A a1 = JSONObject.parseObject(JSONObject.toJSONString(a), A.class);
        System.out.println(a1);
    }

    public static class A {
        private final String name;
        private final long length;

        public A(String name, long length) {
            this.name = name;
            this.length = length;
        }

        public String getName() {
            return name;
        }

        public long getLength() {
            return length;
        }

        @Override
        public String toString() {
            return "A{" +
                    "name='" + name + '\'' +
                    ", length=" + length +
                    '}';
        }
    }

    @Test
    public void test18() throws IOException {
        List<String> lines = Files.readAllLines(Paths.get("C:\\Users\\Thin'k'pa'd\\Desktop\\1.txt"));
        String template = "TYPE_%s(%s, %s%s),\n";
        lines.stream().forEach(line -> {
            String[] split = line.trim().split("\\s+");
            System.out.printf(template, split[0], split[0], "\""+split[1]+"\"", "成功".equals(split[2]) ? ", true" : "");
        });

    }


}
